const { defineConfig } = require('@vue/cli-service')
const path = require('path') //必须引入
const resolve = dir => path.join(__dirname, dir) //必须引入
// const AutoImport = require('unplugin-auto-import/webpack')
const Components = require('unplugin-vue-components/webpack')
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    proxy: {
      '/music': {
        target: 'http://localhost:2023',
        changeOrigin: true
      },
      '/common': {
        target: 'http://localhost:2023',
        changeOrigin: true
      },
      '/public': {
        target: 'http://localhost:2023',
        changeOrigin: true
      },
      '/manage': {
        target: 'http://localhost:2023',
        changeOrigin: true
      }
      // '/upload': {
      //   target: 'http://localhost:2023', // 静态资源代理地址
      //   changeOrigin: true
      // }
    }
  },
  configureWebpack: {
    plugins: [
      require('unplugin-vue-components/webpack')({
        /* options */
      }),
      // AutoImport({
      //   resolvers: [
      //     name => {
      //       if (name === '$message') {
      //         return {
      //           from: 'element-ui',
      //           name: 'Message',
      //           sideEffects: ['element-ui/lib/theme-chalk/base.css', 'element-ui/lib/theme-chalk/message.css']
      //         }
      //       }
      //     }
      //   ]
      // }),
      Components({
        resolvers: [ElementPlusResolver()]
      })
    ]
  },
  css: {
    loaderOptions: {
      sass: {
        // vue3用additionalData
        // vue2用prependData
        additionalData: `
          @import "./src/common/css/theme.scss";
        `
      }
    }
  },
  chainWebpack: config => {
    config.module.rules.delete('svg') //重点:删除默认配置中处理svg,
    config.module
      .rule('svg-sprite-loader')
      .test(/\.svg$/)
      .include.add(resolve('src/icons')) //处理svg目录
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
    config.plugin('define').tap(args => {
      args[0]['process.env'].VUE_APP_BASEURL =
        process.env.NODE_ENV === 'production' ? JSON.stringify('http://localhost:2023') : '""' // 请求前缀
      return args
    })
  }
})
